package com.lishuang.user.controller;

import com.lishuang.basic.exception.BusinessException;
import com.lishuang.basic.util.AjaxResult;
import com.lishuang.user.dto.BinderDto;
import com.lishuang.user.dto.LoginDto;
import com.lishuang.user.service.ILogininfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;


    @ApiOperation(value = "账号登录")
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            return logininfoService.accountLogin(loginDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败：" + e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试!!!");
        }

    }

    @ApiOperation(value = "微信登录")
    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code ){
        try {
            return logininfoService.wechatLogin(code);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登录失败，"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试!!!");
        }
    }

    @ApiOperation(value = "微信绑定接口",notes = "")
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody BinderDto binderDto){
        try {
            return  logininfoService.wechatBinder(binderDto);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("绑定失败，"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试!!!");
        }
    }

    @ApiOperation(value = "账号退出")
    @GetMapping("/logout/{token}")
    public AjaxResult logout(@PathVariable("token") String token){
        try {
            logininfoService.logout(token);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("退出失败：" + e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试!!!");
        }

    }
}
